package com.aso114.project.mvp.activity;

import android.app.ProgressDialog;
import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v4.content.Loader;
import android.view.View;

import com.aso114.project.R;
import com.aso114.project.base.BaseMvpActivity;
import com.aso114.project.base.BaseSimpleActivity;
import com.aso114.project.exam.Question;
import com.aso114.project.exam.QuestionTestActivity;
import com.aso114.project.exam.Result;
import com.aso114.project.mvp.presenter.MainPresenter;
import com.aso114.project.mvp.view.MainView;
import com.aso114.project.util.PresenterFactory;
import com.aso114.project.util.PresenterLoder;
import com.aso114.project.util.dbhelper.QuestionSqliteOpenHelper;

import java.io.IOException;
import java.util.ArrayList;

import butterknife.ButterKnife;
import butterknife.OnClick;


/**
 * 考试题目
 */
public class QuestionActivity extends BaseSimpleActivity {

    public static String SQL = "select * from c1_1 order by random() limit 100";
    ArrayList<Result> results = new ArrayList<>();
    ProgressDialog dialog;
    Intent intent;

    @Override
    protected int getResouseId() {
        return R.layout.question;
    }

    @Override
    protected void initView() {
        intent = new Intent(QuestionActivity.this, QuestionTestActivity.class);
    }


    //随机练习
    public void intent(View view) {
        intent.putExtra(QuestionTestActivity.IS_EXAM, false);
        SQL = "select * from c1_1 order by random() limit 100";
        initData();
    }

    //顺序练习
    public void test(View v) {
        intent.putExtra(QuestionTestActivity.IS_EXAM, false);
        SQL = "select * from c1_1";
        initData();
    }

    //全真模拟
    public void exam(View v) {
        intent.putExtra(QuestionTestActivity.IS_EXAM, true);
        SQL = "select * from c1_1 order by random() limit 100";
        initData();
    }


    public void initData() {
        results.clear();
        dialog = new ProgressDialog(this);
        dialog.setTitle("loading");
        dialog.show();
        new Thread() {
            @Override
            public void run() {
//                SQLiteDatabase database = SQLTools.opendatabase(MainActivity.this);


                SQLiteDatabase database;
                Cursor cursor = null;
                //获取assets目录下的数据库中的所有城市的openHelper
                QuestionSqliteOpenHelper op = new QuestionSqliteOpenHelper(QuestionActivity.this);
//                ArrayList<City> cityList = new ArrayList<City>();

                try {


                    op.createDataBase();
                    database = op.getWritableDatabase();

                    cursor = database.rawQuery(SQL, null);
                    int question = cursor.getColumnIndex("question");
                    int answer = cursor.getColumnIndex("answer");
                    int item1 = cursor.getColumnIndex("item1");
                    int item2 = cursor.getColumnIndex("item2");
                    int item3 = cursor.getColumnIndex("item3");
                    int item4 = cursor.getColumnIndex("item4");
                    int explains = cursor.getColumnIndex("explains");
                    int url = cursor.getColumnIndex("url");
                    while (cursor.moveToNext()) {
                        Result r = new Result();
                        r.setQuestion(cursor.getString(question));
                        r.setAnswer(cursor.getString(answer));
                        r.setItem1(cursor.getString(item1));
                        r.setItem2(cursor.getString(item2));
                        r.setItem3(cursor.getString(item3));
                        r.setItem4(cursor.getString(item4));
                        r.setExplains(cursor.getString(explains));
                        r.setUrl(cursor.getString(url));
                        results.add(r);
                    }

                    Question.result = results;
                    handler.sendEmptyMessage(0);
                    cursor.close();
                    database.close();

                } catch (IOException e) {
                    e.printStackTrace();
                } finally {
//                    cursor.close();
                }

            }
        }.start();
    }

    private Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            dialog.dismiss();
            startActivity(intent);
        }
    };


    @Override
    public void onBackPressed() {
        super.onBackPressed();
    }


    @Override
    protected void getBroadcast(Context context, Intent intent) {

    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // TODO: add setContentView(...) invocation
        ButterKnife.bind(this);
    }

    @OnClick({R.id.layout_title_bar_back_iv, R.id.layout_title_bar_title_tv})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.layout_title_bar_back_iv:
                finish();
                break;
            case R.id.layout_title_bar_title_tv:
                break;
        }
    }
}
